/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/


#include "noteobjectbroker.h"
#include "noteobjecttoolbar.h"

#include "uicapabilities.h"

#include "ui_mainwindow.h"
#include <QtDebug>

NoteObjectBroker::NoteObjectBroker(Ui::MainWindow* ui, NoteObjectToolBar* toolbar)
		:pMainWindow(ui),
		pNoteObjectToolbar(toolbar)
{
}


void NoteObjectBroker::Connect(QObject* object, UICapabilities capabilities)
{
	if (pConnectedObject.data() != object)
	{
		if (pConnectedObject)
		{
			qDebug() << "Broker: Disconnect from earlier object";
			Disconnect(pConnectedObject.data(), UICapabilities());

			pNoteObjectToolbar->disableActions();

		}

		pConnectedObject = object;

		if (capabilities.canDeletion())
		{
			qDebug() << "Broker: - connects the actionDelete()";
			connect(pNoteObjectToolbar, SIGNAL(actionDelete()), object, SLOT(actionDelete()));

			pNoteObjectToolbar->enableDeletion();

		}

		if (capabilities.canLabeling())
		{
			qDebug() << "Broker: connect labeling";
			connect(pNoteObjectToolbar.data(), SIGNAL(actionSetLable(QString,QIcon)), object, SLOT(actionSetLable(QString,QIcon)));

			pNoteObjectToolbar->enableTaging();
		}

	}
	else
		qDebug() << "Connect to same object is ignored";


}


void NoteObjectBroker::Disconnect(QObject* object, UICapabilities capabilities)
{
	disconnect(pNoteObjectToolbar, SIGNAL(actionDelete()), object, SLOT(actionDelete()));
	disconnect(pNoteObjectToolbar.data(), SIGNAL(actionSetLable(QString,QIcon)), object, SLOT(actionSetLable(QString,QIcon)));

	// disconnect(pMainWindow->pBtnDelete, SIGNAL(clicked()), object, SLOT(actionDelete()));

	pConnectedObject = 0;

	pNoteObjectToolbar->disableActions();


}

